package com.example.videomedian;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;

import java.text.SimpleDateFormat;
import java.util.Date;


public class Video2Activity extends AppCompatActivity {

    VideoView video;
    int pos ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video2);



        video=findViewById(R.id.videoView);
        play(getIntent().getIntExtra("video",0));


        Button  video2Return=findViewById(R.id.video2Return);
        video2Return.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                       VideoActivity.list.get(pos).lastTime = stringForTime(video.getCurrentPosition());
                       VideoActivity.adapter.notifyDataSetChanged();
                       finish();

                    }
                }
        );


        Button  btnOther=findViewById(R.id.btnOther);
        btnOther.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    String[]appTitles=new String[VideoActivity.list.size()];

                        for (int i = 0; i < VideoActivity.list.size(); i++) {

                      appTitles[i]=VideoActivity.list.get(i).vName;


                        }

                        final AlertDialog.Builder builder = new AlertDialog.Builder(Video2Activity.this);

                        builder.setTitle("请选择视频：");
                        builder.setItems(appTitles, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                VideoActivity.list.get(pos).lastTime=stringForTime(video.getCurrentPosition());

                                play(which);
                        }
                        });

                        builder.show();

                    }
                }
        );

    }

    private void play(int pos) {
          this.pos=pos;

          video.setVideoPath(VideoActivity.list.get(pos).path);

          MediaController mediaController=new MediaController(Video2Activity.this);
          video.setMediaController(mediaController);
          video.start();

    }


    private String stringForTime(int milliseconds) {

        SimpleDateFormat format=new SimpleDateFormat("HH:mm:ss");
        String thistime=format.format(new Date(milliseconds));
        return  thistime;

    }
}